﻿using NetServer;
using System.Net;
using System.Text;

namespace DeviceController.NetCoreServer.Services
{
    public class DeviceControllerClient : TcpClient
    {
        public event EventHandler? Connected;

        public event EventHandler? Disconnected;

        public event EventHandler<ReceivedEventArgs>? Received;

        public DeviceControllerClient(IPAddress address, int port) : base(address, port)
        {
        }

        /// <summary>
        /// 当接收到数据时进入该方法
        /// </summary>
        /// <param name="buffer">接收缓冲区</param>
        /// <param name="offset">缓冲区偏移</param>
        /// <param name="size">接收到的字节数</param>
        protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            Received?.Invoke(this, new ReceivedEventArgs(buffer.Skip((int)offset).Take((int)size).ToArray()));
        }

        protected override void OnConnected()
        {
            Connected?.Invoke(this, EventArgs.Empty);
        }

        protected override void OnDisconnected()
        {
            Disconnected?.Invoke(this, EventArgs.Empty);
        }
    }
}
